package com.aco.party.web.controller.system;

import com.aco.party.model.po.User;
import com.aco.party.model.po.Visit;
import com.aco.party.model.vo.common.UserWithRoleVo;
import com.aco.party.service.system.UserService;
import com.aco.party.service.system.VisitService;

import javax.annotation.Resource;
import java.util.logging.Logger;

/**
 * Created by flying on 3/12/2016 0012.
 */
public class BaseController {
    private Logger logger = Logger.getLogger(getClass().getName());
    @Resource
    private UserService userService;

    @Resource
    private VisitService visitService;

    protected UserWithRoleVo getCurrentUser(){
        UserWithRoleVo user = userService.getCurrentUser();
        if(user == null){
            user = new UserWithRoleVo();
            user.setId(-1L);
            user.setUsername("anonymous");
            user.setNickName("没有获取到当前登录人");
            return user;
        }
        return user;
    }

    protected void addVisit(Long targetId,String type,String ip){
        Visit visit = new Visit();
        visit.setType(type);
        visit.setTargetId(targetId);
        visit.setVisitUser(getCurrentUser().getUsername());
        visit.setVisitIp(ip);
        visitService.add(visit);
        logger.info("添加访问记录:" + visit.toString());
    }
}
